<template>
	<view>
		<uni-search-bar placeholder="账号/手机号" bgColor="#EEEEEE" @confirm="search" clearButton="auto" class="uni-mt-10" radius="30" cancelButton="none"/>
		<uni-list>
			<uni-list-item v-for="user in users" :title="user.nickname" :thumb="user.faceImg"
				thumb-size="medium">
				<template #footer>
					<button size="mini" type="primary" @click="add(user.id)">添加到通讯录</button>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				users: []
			}
		},
		methods: {
			search(key) {
				this.$http('/user/friend/query','get',{key: key.value}).then(
					succ => {
						this.users = succ.data
					}
				)
			},
			add(friendId) {
				uni.showModal({
					title: '提示',
					content: '确定添加该用户为好友？',
					success: res => {
						if (res.confirm) {
							let data = {
								friendId,
								userId: JSON.parse(uni.getStorageSync('login-user-info')).id
							}
							this.$http('/user/friend/add', 'post', data).then(
								succ => {
									if (succ.code === 200) {
										uni.showToast({
											title: '添加成功'
										})
									}
								}
							)
						} else if (res.cancel) {
							
						}
					}
				})
			}
		}
	}
</script>

<style>

</style>
